from random import randint
import pandas as pd
import numpy as np
import copy
from sklearn import model_selection
import torch


dataframe = pd.read_csv("../log/log1.csv", low_memory=False, index_col=0)
npdata = dataframe.values

def nptolist(npdata):
    l1 = []
    n = 0
    for i in npdata:
        l2 = []
        for j in i:
            if isinstance(j, str):
                l2.append(j)
        l1.append(copy.deepcopy(l2))
        n += 1
    print("***********")
    return l1

def delevent(data):  # 删除活动并返回被删除的活动以及它直接前继与后继
    # l1 = data.copy()
    l1=copy.deepcopy(data)
    l2 = []
    delindexlist=[]
    for i in l1:
        length = len(i)
        r = randint(0, length - 1)
        l2.append(i[r])
        delindexlist.append(r)
        del i[r]
    return l1, l2,delindexlist


def mergedata(d1,d2):
    l=[]
    l1=copy.deepcopy(d1)
    l2=copy.deepcopy(d2)
    for i in l1:
        l.append(i)
    for i in l2:
        l.append(i)
    return l

if __name__ == '__main__':
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print(type(npdata))
    data = nptolist(npdata)
    datatrain, datatest = model_selection.train_test_split(npdata, train_size=0.1)
    datatrain = nptolist(datatrain)
    datatest = nptolist(datatest)
    datatesttrace, datatestlabel, testdelindex = delevent(datatest)
    print(len(datatrain),len(datatest))
    deldata=mergedata(datatrain,datatesttrace)
    print(len(deldata))
    deldata=pd.DataFrame(deldata)
    datatrain=pd.DataFrame(datatrain)
    datatesttrace=pd.DataFrame(datatesttrace)
    deldata.to_csv('deldata.csv')
    datatrain.to_csv('datatrain.csv')
    datatesttrace.to_csv('datatest.csv')

